package com.cheng.utli;

import com.alibaba.fastjson.JSONObject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Redis工具类
 */
@Service
public class RedisUtil {

    @Resource(name="redisTemplate")
    private RedisTemplate redisTemplate;

    /**
     * 批量删除对应的value
     *
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    /**
    * 获得String类型的value值
    * */
    public String getString(String key){
        String result = null;
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        result = (String) operations.get(key);
        return result;
    }

    /**
     * 获得JsonO类型的value值
     * */
    public JSONObject getJson(String key){
        String result = null;
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        result = (String) operations.get(key);
        return  JSONObject.parseObject(result);
    }


    /**
     * 组装REDIS_KEY
     * @param redisKey REDIS KEY头
     * @param otherParam REDIS KEY 后面参数
     * @return redisKey + "_" + pram1 + "_" + pram2 ...
     */
    public String formatRedisKey(String redisKey, Object... otherParam) {
        StringBuilder result = new StringBuilder();
        result.append(redisKey.toUpperCase());
        for (Object obj : otherParam) {
            if (obj == null) {
                continue;
            }
            result.append("_").append(String.valueOf(obj).toUpperCase());
        }
        return result.toString();
    }


    /**
     * 批量删除key
     *
     * @param pattern
     */
    public void removePattern(final String pattern) {
        Set<Serializable> keys = redisTemplate.keys(pattern);
        if (keys.size() > 0)
            redisTemplate.delete(keys);
    }

    /**
     * 删除对应的value
     *
     * @param key
     */
    public void remove(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }

    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public Object get(final String key) {
        Object result = null;
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        return result;
    }

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * incrBy
     *
     * @param key
     * @return
     */

    public boolean incrBy(final String key) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.increment(key);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
